package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.system.bo.ZhcaInformationAddBo;
import com.ruoyi.system.bo.ZhcaInformationQueryBo;
import com.ruoyi.system.service.IZhcaInformationService;
import com.ruoyi.system.vo.ZhcaInformationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * 小程序Controller
 *
 * @author ruoyi
 * @date 2021-07-25
 */
@Validated
@Api(value = "资讯信息", tags = {"资讯信息"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/pacinformation")
public class InformationController extends BaseController {

	private final IZhcaInformationService iZhcaInformationService;
    /**
     * 爬取长安官网信息
     */
    @ApiOperation("首页资讯信息")
    //@PreAuthorize("@ss.hasPermi('system:smallapp:list')")
    @GetMapping("/list")
    public void list() {
	    try {
		    todgweb();//首页资讯
			todouban();//同城
	    } catch (Exception e) {
		    //try catch处理防止这边报错影响后续爬虫
	    }
	    iZhcaInformationService.reptileChangAnTownLibrary();//走进长安->长安镇图书馆
	    iZhcaInformationService.reptileChangAnCultureAndEducation();//走进长安->长安文化教育
	    iZhcaInformationService.reptileHappyChangAn();//欢乐长安
	    iZhcaInformationService.reptileChangAnEmergencyManagement();//长安应急管理
	    iZhcaInformationService.reptileSafeChangAn();//平安长安
	    iZhcaInformationService.reptileHappyWoman();//欢乐巾帼
    }


	public static void main(String[] args) {
		//todouban();
	}


	public void todgweb(){
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();
		HttpGet httpGet = new HttpGet("http://www.dg.gov.cn/changan/jjca/index.html");
		CloseableHttpResponse response = null;
		try {
			// 由客户端执行(发送)Get请求
			response = httpClient.execute(httpGet);
			// 从响应模型中获取响应实体
			HttpEntity responseEntity = response.getEntity();
			//HTML内容

			//System.out.println("响应状态为:" + response.getStatusLine());
			if (responseEntity != null) {
				//System.out.println("响应内容长度为:" + responseEntity.getContentLength());
				//System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
				String htmlcontent = EntityUtils.toString(responseEntity);
				//数组
				String[] con_two_1s = htmlcontent.split("con_two_1");
				String titlemesurl = con_two_1s[1].split("href")[1].split("\"")[1];
				String titlemesurl2 = con_two_1s[1].split(titlemesurl)[2].split("href")[1].split("\"")[1];;;
//				String titlemesurl = "http://www.dg.gov.cn/changan/jjca/cayw/content/post_3832053.html";
//				String titlemesurl2 = "http://www.dg.gov.cn/changan/jjca/cayw/content/post_3832030.html";
//				String titlemesurl3 = "http://www.dg.gov.cn/changan/jjca/cayw/content/post_3831477.html";
//				String titlemesurl4 = "http://www.dg.gov.cn/changan/jjca/cayw/content/post_3831474.html";
				getTitle(titlemesurl,1);
				getTitle(titlemesurl2,1);
//				getTitle(titlemesurl3,1);
//				getTitle(titlemesurl4,1);
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				// 释放资源
				if (httpClient != null) {
					httpClient.close();
				}
				if (response != null) {
					response.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}


	public void getTitle(String url,int state) {
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();
		HttpGet httpGet = new HttpGet(url);
		CloseableHttpResponse response = null;
		try {
			// 由客户端执行(发送)Get请求
			response = httpClient.execute(httpGet);
			// 从响应模型中获取响应实体
			HttpEntity responseEntity = response.getEntity();
			//HTML内容

			//System.out.println("响应状态为:" + response.getStatusLine());
			if (responseEntity != null) {
			//System.out.println("响应内容长度为:" + responseEntity.getContentLength());
			//	System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
				String htmlcontent = EntityUtils.toString(responseEntity);
				//System.out.println("响应内容为:" + htmlcontent);
				String[] con_two_1s = htmlcontent.split("<td height=\"100\" align=\"center\" style=\"color:#C00; font-size:25px; font-weight:bold;\">");
				String title = con_two_1s[1].split("</td>")[0].trim();
				System.out.println("响应内容为:" + title);
				String[] con_two_2s = htmlcontent.split("img class=\"nfw-cms-img\"");
				String pic = con_two_2s[1].split("src=")[1].split("alt=")[0];
				//String pic = con_two_2s[1].split("src")[0].split("src=\"")[1].split("\"")[0];
				System.out.println("响应内容为:" + pic);
				String[] con_two_3s = htmlcontent.split("<td id=\"zoom\"");
				String content = con_two_3s[1];
				String newContent = "<td id=\"zoom\""+content.split("<table width=\"1060\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">")[0];
				ZhcaInformationQueryBo 	zhcaInformationQueryBo = new ZhcaInformationQueryBo();
				zhcaInformationQueryBo.setTitle(title);
				zhcaInformationQueryBo.setPageNum(1);
				zhcaInformationQueryBo.setPageSize(10);
				List<ZhcaInformationVo> zhcaInformationVoTableDataInfo = iZhcaInformationService.queryList(zhcaInformationQueryBo);

				//System.out.println("zhcaInformationVoTableDataInfo.getTotal()"+zhcaInformationVoTableDataInfo.size());
				if(zhcaInformationVoTableDataInfo.size()==0){
					ZhcaInformationAddBo ZhcaInformationAddBo = new ZhcaInformationAddBo();
					ZhcaInformationAddBo.setTitle(title);
					ZhcaInformationAddBo.setPic(pic);
					ZhcaInformationAddBo.setContent(newContent);
					ZhcaInformationAddBo.setState(Long.valueOf(state));
					iZhcaInformationService.insertByAddBo(ZhcaInformationAddBo);
				}


			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				// 释放资源
				if (httpClient != null) {
					httpClient.close();
				}
				if (response != null) {
					response.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}


	public  void todouban(){
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();
		HttpGet httpGet = new HttpGet("https://www.douban.com/event/search?search_text=%E9%95%BF%E5%AE%89%E9%95%87&loc=dongguan");
		httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3");
		CloseableHttpResponse response = null;
		try {
			// 由客户端执行(发送)Get请求
			response = httpClient.execute(httpGet);
			// 从响应模型中获取响应实体
			HttpEntity responseEntity = response.getEntity();
			//HTML内容

			//System.out.println("响应状态为:" + response.getStatusLine());
			if (responseEntity != null) {
				//System.out.println("响应内容长度为:" + responseEntity.getContentLength());
				//System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
				String htmlcontent = EntityUtils.toString(responseEntity);
				//数组
				String[] con_two_1s = htmlcontent.split("<img src=\"");
				String picurl = con_two_1s[1].split("\"")[0];
				System.out.println("响应内容为:" + picurl);

				String[] con_two_2s = htmlcontent.split("event-title");
				String title = con_two_2s[1].split("title")[1].split("\"")[1].trim(); ;
				System.out.println("响应内容为:" + title);

				String[] con_two_3s = htmlcontent.split("class=\"search-list-item\"");
				String content = con_two_3s[1].split("<a")[0];
				System.out.println("响应内容为:" + content);

				ZhcaInformationQueryBo 	zhcaInformationQueryBo = new ZhcaInformationQueryBo();
				zhcaInformationQueryBo.setTitle(title);
				zhcaInformationQueryBo.setPageNum(1);
				zhcaInformationQueryBo.setPageSize(10);
				List<ZhcaInformationVo> zhcaInformationVoTableDataInfo = iZhcaInformationService.queryList(zhcaInformationQueryBo);
				System.out.println("zhcaInformationVoTableDataInfo.getTotal()"+zhcaInformationVoTableDataInfo.size());
				if (zhcaInformationVoTableDataInfo.size()==0){
					ZhcaInformationAddBo ZhcaInformationAddBo = new ZhcaInformationAddBo();
					ZhcaInformationAddBo.setTitle(title);
					ZhcaInformationAddBo.setPic(picurl);
					ZhcaInformationAddBo.setContent(content);
					ZhcaInformationAddBo.setState(Long.valueOf(3));
					iZhcaInformationService.insertByAddBo(ZhcaInformationAddBo);
				}

			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				// 释放资源
				if (httpClient != null) {
					httpClient.close();
				}
				if (response != null) {
					response.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}




}
